home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / emcs1857 / 1857sr~1.zoo / src / sorttab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  9.9 KB  |  364 lines

  1. /* GNU Emacs routines to deal with sort tables.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU Emacs General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU Emacs, but only under the conditions described in the
  15. GNU Emacs General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU Emacs so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21. /* Written by Howard Gayle.  See chartab.c for details. */
  22.  
  23. #include "config.h"
  24. #include "lisp.h"
  25. #include "buffer.h"
  26. #include "etctab.h"
  27. #include "sorttab.h"
  28.  
  29. Lisp_Object Qsort_table_p;
  30. DEFUN ("sort-table-p", Fsort_table_p, Ssort_table_p, 1, 1, 0,
  31.    "Return t iff ARG is a sort table.")
  32. (obj)
  33. Lisp_Object obj;
  34. {
  35. return ((XTYPE (obj) == Lisp_Sorttab) ? Qt : Qnil);
  36. }
  37.  
  38. static Lisp_Object
  39. check_sort_table (obj)
  40. Lisp_Object obj;
  41. {
  42. register Lisp_Object tem;
  43.  
  44. while (tem = Fsort_table_p (obj), NULL (tem))
  45.    obj = wrong_type_argument (Qsort_table_p, obj, 0);
  46. return (obj);
  47. }   
  48.  
  49. /* Convert the given Lisp_Sorttab to a Lisp_Object. */
  50. static Lisp_Object
  51. enlisp_sort_table (sp)
  52. register struct Lisp_Sorttab *sp;
  53. {
  54. register Lisp_Object z; /* Return. */
  55.  
  56. if (sp == NULL_SORT_TABLE)
  57.    z = Qnil;
  58. else
  59.    XSET (z, Lisp_Sorttab, sp);
  60. return (z);
  61. }
  62.  
  63. DEFUN ("case-distinct-table",
  64.    Fcase_distinct_table, Scase_distinct_table, 0, 0, 0,
  65.    "Return the case-distinct sort table of the current buffer.")
  66. ()
  67. {
  68. return (enlisp_sort_table (current_buffer->case_distinct_table_v));
  69. }
  70.  
  71. DEFUN ("case-fold-table", Fcase_fold_table, Scase_fold_table, 0, 0, 0,
  72.    "Return the case-fold sort table of the current buffer.")
  73. ()
  74. {
  75. return (enlisp_sort_table (current_buffer->case_fold_table_v));
  76. }
  77.  
  78. DEFUN ("standard-case-distinct-table",
  79.    Fstandard_case_distinct_table, Sstandard_case_distinct_table, 0, 0, 0,
  80.    "Return the standard case-distinct sort table.\n\
  81. This is the one used for new buffers.")
  82. ()
  83. {
  84. return (enlisp_sort_table (buffer_defaults.case_distinct_table_v));
  85. }
  86.  
  87. DEFUN ("standard-case-fold-table",
  88.    Fstandard_case_fold_table, Sstandard_case_fold_table, 0, 0, 0,
  89.    "Return the standard case-fold sort table.\n\
  90. This is the one used for new buffers.")
  91. ()
  92. {
  93. return (enlisp_sort_table (buffer_defaults.case_fold_table_v));
  94. }
  95.  
  96. /* Store a case-distinct sort table.  Check for errors. */
  97. static Lisp_Object
  98. set_case_distinct_table (p, t)
  99. struct Lisp_Sorttab **p; /* Points to where to store the sort table. */
  100. register Lisp_Object t;  /* The sort table as a Lisp object. */
  101. {
  102. if (NULL (t))
  103.    *p = NULL_SORT_TABLE;
  104. else
  105.    {
  106.    t = check_sort_table (t);
  107.    *p = XSORTTAB (t);
  108.    }
  109. return (t);
  110. }
  111.  
  112. /* Store a case-fold sort table.  Check for errors. */
  113. static Lisp_Object
  114. set_case_fold_table (p, t)
  115. struct Lisp_Sorttab **p; /* Points to where to store the sort table. */
  116. register Lisp_Object t;  /* The sort table as a Lisp object. */
  117. {
  118. t = check_sort_table (t);
  119. *p = XSORTTAB (t);
  120. return (t);
  121. }
  122.  
  123. DEFUN ("set-case-distinct-table",
  124.    Fset_case_distinct_table, Sset_case_distinct_table, 1, 1, 0,
  125.    "Select a new case-distinct sort table for the current buffer.\n\
  126. One argument, a sort table.")
  127. (table)
  128. Lisp_Object table;
  129. {
  130. return (set_case_distinct_table (¤t_buffer->case_distinct_table_v, table));
  131. }
  132.  
  133. DEFUN ("set-case-fold-table",
  134.    Fset_case_fold_table, Sset_case_fold_table, 1, 1, 0,
  135.    "Select a new case-fold sort table for the current buffer.\n\
  136. One argument, a sort table.")
  137. (table)
  138. Lisp_Object table;
  139. {
  140. return (set_case_fold_table (¤t_buffer->case_fold_table_v, table));
  141. }
  142.  
  143. DEFUN ("set-standard-case-distinct-table", Fset_standard_case_distinct_table,
  144.    Sset_standard_case_distinct_table, 1, 1, 0,
  145.    "Select a new standard case-distinct sort table.\n\
  146. This does not change the sort tables of any existing buffers.\n\
  147. One argument, a sort table.")
  148. (table)
  149. Lisp_Object table;
  150. {
  151. return (set_case_distinct_table (&buffer_defaults.case_distinct_table_v, table));
  152. }
  153.  
  154. DEFUN ("set-standard-case-fold-table", Fset_standard_case_fold_table,
  155.    Sset_standard_case_fold_table, 1, 1, 0,
  156.    "Select a new standard case-fold sort table.\n\
  157. This does not change the sort tables of any existing buffers.\n\
  158. One argument, a sort table.")
  159. (table)
  160. Lisp_Object table;
  161. {
  162. return (set_case_fold_table (&buffer_defaults.case_fold_table_v, table));
  163. }
  164.  
  165. /* Return the sort table for the current buffer. */
  166. struct Lisp_Sorttab *
  167. current_sort_table ()
  168. {
  169. register struct buffer *bp = current_buffer;
  170.  
  171. return ((NULL (bp->case_fold_search))
  172.     ? bp->case_distinct_table_v
  173.     : bp->case_fold_table_v);
  174. }
  175.  
  176. /* Return the equivalence class table of the current sort table. */
  177. char_t *
  178. current_equiv_class_table ()
  179. {
  180. register struct Lisp_Sorttab *sp = current_sort_table ();
  181.  
  182. return ((sp == NULL_SORT_TABLE) ? ((char_t *) 0) : sp->srt_ec);
  183. }
  184.  
  185. DEFUN ("make-sort-table", Fmake_sort_table, Smake_sort_table, 1, 1, 0,
  186.    "Return a new sort table.  Argument is a list of elements in\n\
  187. increasing order.  Each element is a list representing an\n\
  188. equivalence class.")
  189. (lst)
  190. Lisp_Object lst;
  191. {
  192. register int                      i;
  193. register struct Lisp_Sorttab *nt; /* New sort table. */
  194. register Lisp_Object p; /* Steps through lst. */
  195. register Lisp_Object ce; /* Current sublist. */
  196. register char_t c; /* Current char in sublist. */
  197. register char_t cec = 0; /* Current equivalence class. */
  198. register Lisp_Object z; /* Return. */
  199. char_t cvr[256]; /* Flag set if char covered. */
  200.  
  201. CHECK_CONS (lst, 1);
  202. z = make_etc_table (sizeof (struct Lisp_Sorttab), Lisp_Sorttab);
  203. nt = XSORTTAB (z);
  204. for (i = 0; i <= 255; ++i)
  205.    cvr[i] = 0;
  206. i = 0;
  207. for (p = lst; !NULL (p); p = Fcdr (p))
  208.    {
  209.    ce = Fcar (p);
  210.    CHECK_CONS (ce, 2);
  211.    nt->srt_dope[cec].ec_lo = (char_t) i;
  212.    for (; !NULL (ce); ce = Fcdr (ce))
  213.       {
  214.       c = get_char_arg (Fcar (ce));
  215.       if (cvr[c]) arg_out_of_range (lst);
  216.       nt->srt_ec[c] = cec;
  217.       nt->srt_chars[i++] = c;
  218.       ++cvr[c];
  219.       }
  220.    nt->srt_dope[cec++].ec_hi = (char_t) (i - 1);
  221.    }
  222. for (i = 0; i <= 255; ++i)
  223.    if (!cvr[i]) arg_out_of_range (lst);
  224. return (z);
  225. }
  226.  
  227. DEFUN ("get-sort-table-ec",
  228.    Fget_sort_table_ec, Sget_sort_table_ec, 2, 2, 0,
  229.    "Return the equivalence class containing character CHAR in\n\
  230. sort table TABLE.  The equivalence class is represented as a string.")
  231. (ch, table)
  232. Lisp_Object ch;
  233. register Lisp_Object table;
  234. {
  235. register struct Lisp_Sorttab *sp; /* Sort table. */
  236. register int ec;                     /* Equivalence class number. */
  237. register int l;                         /* Index of first char in EC. */
  238.  
  239. table = check_sort_table (table);
  240. sp = XSORTTAB (table);
  241. ec = sp->srt_ec[get_char_arg (ch)];
  242. l = sp->srt_dope[ec].ec_lo;
  243. return (make_string (&sp->srt_chars[l], sp->srt_dope[ec].ec_hi - l + 1));
  244. }
  245.  
  246. DEFUN ("get-sort-table-ec-num",
  247.    Fget_sort_table_ec_num, Sget_sort_table_ec_num, 2, 2, 0,
  248.    "Return the equivalence class number of character CHAR in\n\
  249. sort table TABLE.")
  250. (ch, table)
  251. Lisp_Object ch;
  252. register Lisp_Object table;
  253. {
  254. register Lisp_Object z;
  255.  
  256. table = check_sort_table (table);
  257. XFASTINT (z) = XSORTTAB (table)->srt_ec[get_char_arg (ch)];
  258. return (z);
  259. }
  260.  
  261. DEFUN ("string-lessp*", Fstring_lesspX, Sstring_lesspX, 2, 3, 0,
  262.    "T iff string S1 is less than string S2, according to sort\n\
  263. table TABLE (default current sort table).")
  264. (s1, s2, table)
  265. Lisp_Object s1, s2;
  266. Lisp_Object table;
  267. {
  268. register int i;
  269. register char_t *p1, *p2;
  270. register int end;
  271. register char_t *tt; /* Equivalence class table. */
  272. register char_t t1, t2; /* Translated characters. */
  273. Lisp_Object Fstring_lessp ();
  274.  
  275. if (NULL (table))
  276.    {
  277.    tt = current_equiv_class_table ();
  278.    if (tt == ((char_t *) 0)) return (Fstring_lessp (s1, s2));
  279.    }
  280. else
  281.    {
  282.    table = check_sort_table (table);
  283.    tt = XSORTTAB (table)->srt_ec;
  284.    }
  285. CHECK_STRING (s1, 0);
  286. CHECK_STRING (s2, 1);
  287. p1 = XSTRING (s1)->data;
  288. p2 = XSTRING (s2)->data;
  289. end = XSTRING (s1)->size;
  290. if (end > XSTRING (s2)->size) end = XSTRING (s2)->size;
  291. for (i = 0; i < end; i++)
  292.    {
  293.    t1 = tt[p1[i]];
  294.    t2 = tt[p2[i]];
  295.    if (t1 != t2) return ((t1 < t2) ? Qt : Qnil);
  296.    }
  297. return ((i < XSTRING (s2)->size) ? Qt : Qnil);
  298. }
  299.  
  300. init_sort_table_once ()
  301. {
  302. register struct Lisp_Sorttab *sp;
  303. register int c; /* Current char. */
  304. register int i = 0;
  305. register Lisp_Object z;
  306. register int cec = 0; /* Current equivalence class. */
  307.  
  308. Fset_standard_case_distinct_table (Qnil);
  309.  
  310. z = make_etc_table (sizeof (struct Lisp_Sorttab), Lisp_Sorttab);
  311. sp = XSORTTAB (z);
  312. for (c = 0; c < 'A'; ++c)
  313.    {
  314.    sp->srt_ec[c] = cec;
  315.    sp->srt_dope[cec].ec_lo = (char_t) i;
  316.    sp->srt_dope[cec++].ec_hi = (char_t) i;
  317.    sp->srt_chars[i++] = c;
  318.    }
  319. for (; c <= 'Z'; ++c)
  320.    {
  321.    sp->srt_ec[c] = cec;
  322.    sp->srt_ec[c - 'A' + 'a'] = cec;
  323.    sp->srt_dope[cec].ec_lo = (char_t) i;
  324.    sp->srt_chars[i++] = c;
  325.    sp->srt_dope[cec++].ec_hi = (char_t) i;
  326.    sp->srt_chars[i++] = c - 'A' + 'a';
  327.    }
  328. for (; c < 'a'; ++c)
  329.    {
  330.    sp->srt_ec[c] = cec;
  331.    sp->srt_dope[cec].ec_lo = (char_t) i;
  332.    sp->srt_dope[cec++].ec_hi = (char_t) i;
  333.    sp->srt_chars[i++] = c;
  334.    }
  335. for (c = '{'; c <= 255; ++c)
  336.    {
  337.    sp->srt_ec[c] = cec;
  338.    sp->srt_dope[cec].ec_lo = (char_t) i;
  339.    sp->srt_dope[cec++].ec_hi = (char_t) i;
  340.    sp->srt_chars[i++] = c;
  341.    }
  342. Fset_standard_case_fold_table (z);
  343. }
  344.  
  345. syms_of_sort_table ()
  346. {
  347. Qsort_table_p = intern ("sort-table-p");
  348. staticpro (&Qsort_table_p);
  349.  
  350. defsubr (&Ssort_table_p);
  351. defsubr (&Scase_distinct_table);
  352. defsubr (&Scase_fold_table);
  353. defsubr (&Sstandard_case_distinct_table);
  354. defsubr (&Sstandard_case_fold_table);
  355. defsubr (&Sset_case_distinct_table);
  356. defsubr (&Sset_case_fold_table);
  357. defsubr (&Sset_standard_case_distinct_table);
  358. defsubr (&Sset_standard_case_fold_table);
  359. defsubr (&Smake_sort_table);
  360. defsubr (&Sget_sort_table_ec);
  361. defsubr (&Sget_sort_table_ec_num);
  362. defsubr (&Sstring_lesspX);
  363. }
  364.